package com.cssl.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.cssl.config.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class PayController {
    @Resource
    private AlipayConfig alipayConfig;

    @RequestMapping("/pay")
    public String oderConfirm(@RequestParam Map<String,Object> map) {
        //初始化AlipClient
        AlipayClient alipayClient=new DefaultAlipayClient(alipayConfig.gatewayUrl,
                alipayConfig.app_id,alipayConfig.merchant_private_key,"json",
                alipayConfig.charset,alipayConfig.alipay_public_key,
                alipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //登成功后支付宝会自动跳转到我设置的页面中去
        alipayRequest.setReturnUrl(alipayConfig.return_url);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = map.get("orderid").toString();
        //付款金额，必填
        String total_amount = map.get("moneys").toString();
        //订单名称，必填
        String subject = map.get("oname").toString();
        //商品描述，可空
        String body = map.get("remark").toString();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\""
                + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result= null;
        try {
            result=alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

}
